Extension { #name : 'Context' }

{ #category : '*OpalCompiler-Core' }
Context >> astScope [
	"Return the scope of the method or the block of this context.
	Note: if the compiler would not model scopes for optimized blocks, this would just just be the
	scope of the compiledCode ast.
	We instead use sourceNodeExecuted and then get the the block or methodnode on the AST level.
	For the topmost context, sourceNodeExecuted is off by one but that is not a problem as
	we still end up getting the scope we want"

	| node |
	node :=  self sourceNodeExecuted.
	"if we get back a block, this is a block that just got pushed. We are not interested in
	that but it's outer block"
	^node isBlock
		ifTrue: [ node parent methodOrBlockNode scope ]
		ifFalse: [node methodOrBlockNode scope ]
]

{ #category : '*OpalCompiler-Core' }
Context >> compiler [
	"Return a compiler set up to parse/compile/evaluate scripts on the current context.
	The compiler is also setup according to the class of the receiver"

	^ self receiver class compiler context: self
]

{ #category : '*OpalCompiler-Core' }
Context >> lookupVar: aSymbol [
	^ self astScope lookupVar: aSymbol
]

{ #category : '*OpalCompiler-Core' }
Context >> sourceNode [
	"Return the source node that created the method or the block of this context"

	^ closureOrNil
		ifNil: [ self compiledCode sourceNode ]
		ifNotNil: [ closureOrNil sourceNode ]
]

{ #category : '*OpalCompiler-Core' }
Context >> sourceNodeExecuted [
	"When down in the stack, I return the node that executed"
	^ method sourceNodeForPC: self executedPC
]
